<?php

namespace App\Web\Model;

use App\Web\Facades\UserAuth;
use EchoPHP\Facades\Db;
use EchoPHP\Validator\Validator;


/**
 * 用户管理相关业务逻辑
 * @package App\Web\Model
 */
class MyResume extends Model {

    /**
     * 查询总记录数
     * @param array $search
     * @return array
     */
    public function getMyTaskCount($search = []) {
        $field = 'count(1) as count';
        return $this->getMyTask($field, $search)[0]['count'];
    }

    /**
     * 查询总记录数
     * @param array $search
     * @return array
     */
    public function getDownLoadTaskCount($search = []) {
        $field = 'count(1) as count';
        return $this->getDownLoadTask($field, $search)[0]['count'];
    }

    /**
     * Notes:执行查询
     * @param $field
     * @param array $search
     * @param int $rowFrom
     * @param int $rowTo
     * @param string $orderBy
     * @return array
     */
    public function getMyTask($field, $search = [], $rowFrom = 0, $rowTo = 0, $orderBy = '') {
        $user = UserAuth::getLoginInfo();
        $sql = 'SELECT ' . $field . ' FROM ResumeOCRTask';
        $where = '';
        $var = [];

        //查询用户ID
        if ($user['issuper'] != 1) {
            $where .= ' creator = ? ';
            $var[] = $search['creator'];
        } else {
            $where .= ' 1=1 ';
        }

        if (isset($search['id']) && !empty($search['id'])) {
            $where .= ' and id = ? ';
            $var[] = $search['id'];
        }

        if (isset($search['attachname']) && !empty($search['attachname'])) {
            $where .= ' and attachname like ? ';
            $var[] = "%" . $search['attachname'] . "%";
        }

        if (isset($search['source']) && !empty($search['source'])) {
            $where .= ' and source = ? ';
            $var[] = $search['source'];
        }

        if (isset($search['status']) && !empty($search['status']) && $search['status'] != '00') {
            $where .= ' and status = ? ';
            $var[] = $search['status'];
        }

        if (isset($search['createdatebegin']) && !empty($search['createdatebegin'])) {
            $where .= ' and createdate >= ? ';
            $var[] = $search['createdatebegin'] . ' 00:00:00';
        }
        if (isset($search['createdateend']) && !empty($search['createdateend'])) {
            $where .= ' and createdate <= ? ';
            $var[] = $search['createdateend'] . ' 23:59:59';
        }

        $sql .= ' WHERE ' . $where;

        $sql .= empty($orderBy) ? ' ORDER BY id DESC' : (' ORDER BY ' . $orderBy);

        return Db::select($sql, $var, $rowFrom, $rowTo);
    }

    /**
     * Notes:执行查询
     * @param $field
     * @param array $search
     * @param int $rowFrom
     * @param int $rowTo
     * @param string $orderBy
     * @return array
     */
    public function getDownLoadTask($field, $search = [], $rowFrom = 0, $rowTo = 0, $orderBy = '') {
        $user = UserAuth::getLoginInfo();
        $sql = 'SELECT ' . $field . ' FROM ResumeExport';
        $where = '';
        $var = [];

        //查询用户ID
        if ($user['issuper'] != 1) {
            $where .= ' creator = ? ';
            $var[] = $search['creator'];
        } else {
            $where .= ' 1=1 ';
        }

        if (isset($search['id']) && !empty($search['id'])) {
            $where .= ' and id = ? ';
            $var[] = $search['id'];
        }

        if (isset($search['templateid']) && !empty($search['templateid'])) {
            $where .= ' and templateid = ? ';
            $var[] = $search['templateid'];
        }

        if (isset($search['attachname']) && !empty($search['attachname'])) {
            $where .= ' and attachname like ? ';
            $var[] = "%" . $search['attachname'] . "%";
        }

        if (isset($search['status']) && !empty($search['status']) && $search['status'] != '00') {
            $where .= ' and status = ? ';
            $var[] = $search['status'];
        }

        if (isset($search['createdatebegin']) && !empty($search['createdatebegin'])) {
            $where .= ' and createdate >= ? ';
            $var[] = $search['createdatebegin'] . ' 00:00:00';
        }
        if (isset($search['createdateend']) && !empty($search['createdateend'])) {
            $where .= ' and createdate <= ? ';
            $var[] = $search['createdateend'] . ' 23:59:59';
        }
        $sql .= ' WHERE ' . $where;

        $sql .= empty($orderBy) ? ' ORDER BY id DESC' : (' ORDER BY ' . $orderBy);

        return Db::select($sql, $var, $rowFrom, $rowTo);
    }


    /**
     * 查询总记录数
     * @param array $search
     * @return array
     */
    public function getUploadResumeCount($search = []) {
        $field = 'count(1) as count';
        return $this->getUploadResume($field, $search)[0]['count'];
    }

    public function getUploadResume($field, $search = [], $rowFrom = 0, $rowTo = 0, $orderBy = '') {
        $user = UserAuth::getLoginInfo();
        $sql = 'SELECT ' . $field . ' FROM UploadResume';
        $where = '';
        $var = [];

        //查询用户ID
        if ($user['issuper'] != 1 && isset($search['oprid'])) {
            $where .= ' oprid = ? ';
            $var[] = $search['oprid'];
        } else {
            $where .= ' 1=1 ';
        }

        if (isset($search['id']) && !empty($search['id'])) {
            $where .= ' and id = ? ';
            $var[] = $search['id'];
        }

        if (isset($search['taskid']) && $search['taskid'] > 0) {
            $where .= ' and taskid = ? ';
            $var[] = $search['taskid'];
        }

        if (isset($search['status']) && !empty($search['status']) && $search['status'] != '00') {
            $where .= ' and status = ? ';
            $var[] = $search['status'];
        }

        if (isset($search['createdatebegin']) && !empty($search['createdatebegin'])) {
            $where .= ' and createdate >= ? ';
            $var[] = $search['createdatebegin'] . ' 00:00:00';
        }
        if (isset($search['createdateend']) && !empty($search['createdateend'])) {
            $where .= ' and createdate <= ? ';
            $var[] = $search['createdateend'] . ' 23:59:59';
        }

        $sql .= ' WHERE ' . $where;
        $sql .= empty($orderBy) ? ' ORDER BY id DESC' : (' ORDER BY ' . $orderBy);

        return Db::select($sql, $var, $rowFrom, $rowTo);
    }


    public function uploadResume($attachInfo, $status = '01') {
        $startDate = $status == '05' ? "Now()" : "0";
        $sql = "INSERT INTO ResumeOCRTask (attachname,filepath,source,totalcount,analysisedcount,creator,createdate,updatedate,status,startdate,enddate,extraparam) 
                values (?,   ?,   ?,     ?,         ?,               ?,     Now(),      Now(),     ?,     " . $startDate . ",        0,     ?)";
        $var = [$attachInfo['attachname'], $attachInfo['filepath'], $attachInfo['source'], 0, 0, $attachInfo['creator'], $status, $attachInfo['extraparam']];
        $result = Db::insert($sql, $var);
        if (!$result) {
            return false;
        }
        $taskId = Db::getLastInsertId();
        return $taskId;
    }

    public function createDownLoadTask($taskInfo, $status = '01') {
        $sql = "INSERT INTO ResumeExport (creator,createdate,updatedate,status,extraparams,totalcount,processedcount,templateid) 
                                  values (?,       Now(),     Now(),    ?,     ?,          ?,         0,             ?)";
        $var = [$taskInfo['creator'], $status, $taskInfo['extraparam'], $taskInfo['totalcount'], $taskInfo['templateid']];
        $result = Db::insert($sql, $var);
        if (!$result) {
            return false;
        }
        $taskId = Db::getLastInsertId();
        return $taskId;
    }

    public function getResumeBaseAttach($search) {
        $sql = 'SELECT attachid FROM UploadResume where resumeid=? and taskid=? ';
        $var = array();

        $var[] = $search['resumeid'];
        $var[] = $search['taskid'];
        //查询用户ID
        $user = UserAuth::getLoginInfo();
        if ($user['issuper'] != 1) {
            $sql .= ' and oprid=? ';
            $var[] = $search['creator'];
        }

        $result = Db::select($sql, $var);
        if (empty($result) || !isset($result[0]['attachid'])) {
            return array();
        }

        $sql = 'SELECT filepath,attachname FROM Attach where attachid=? ';
        $var = array();
        $var[] = $result[0]['attachid'];
        //查询用户ID
        if ($user['issuper'] != 1) {
            $sql .= ' and creator=? ';
            $var[] = $search['creator'];
        }

        $result = Db::select($sql, $var);

        if (empty($result) || !isset($result[0]['filepath'])) {
            return array();
        }
        return $result;
    }

    public function DictSource() {
        $taskSource = array();
        $taskSource['1'] = '猎聘自行下载';
        $taskSource['2'] = '猎聘供应商下载';
        $taskSource['3'] = '智联卓聘';
        $taskSource['4'] = '拉勾';
        $taskSource['5'] = '前程无忧';
        $taskSource['10'] = '智联招聘';
        $taskSource['11'] = '智联金领';
        $taskSource['12'] = '模板导入';
        $taskSource['13'] = '手工录入';
        $taskSource['20'] = '机器学习';
        return $taskSource;
    }

    public function DictStatus() {
        $taskStatus = array();
        $taskStatus['01'] = '待识别';
        $taskStatus['05'] = '识别中';
        $taskStatus['07'] = '已识别';
        $taskStatus['08'] = '识别失败';
        $taskStatus['09'] = '删除';
        return $taskStatus;
    }

    public function DownLoadStatus() {
        $taskStatus = array();
        $taskStatus['01'] = '待导出';
        $taskStatus['05'] = '导出中';
        $taskStatus['07'] = '已完成';
        $taskStatus['08'] = '导出失败';
        $taskStatus['09'] = '删除';
        return $taskStatus;
    }

    public function DictExportTemplate() {
        $templateInfo = array();
        $templateInfo['7'] = '仅索引';                //仅索引
        $templateInfo['6'] = 'Excel格式';             //Excel格式
        $templateInfo['5'] = 'Word格式';              //Word格式
        $templateInfo['1'] = '广州-重点岗位推荐专用';   //简历推荐-重点岗位推荐专用（前程无忧人才推荐报告）
        $templateInfo['2'] = '深圳-候选人推荐报告';     //定制化-候选人推荐报告
        $templateInfo['3'] = '北京-前程无忧推荐报告';   //前程无忧推荐报告
        $templateInfo['4'] = '上海-简历推荐报告';       //简历推荐报告
        return $templateInfo;
    }

    /**
     * Notes:执行查询
     * @param $field
     * @param array $search
     * @param int $rowFrom
     * @param int $rowTo
     * @param string $orderBy
     * @return array
     */
    public function getMyDownloadResume($userID, $resumeID, $startDate, $endDate, $rowFrom, $rowTo) {
        $sql = 'SELECT resumeid,createdate FROM ResumeDownload';
        $where = '';
        $var = [];

        //查询用户ID
        $user = UserAuth::getLoginInfo();
        $where = $var = [];
        //查询用户ID
        if ($user['issuper'] != 1) {
            $where[] = ' oprid = ? ';
            $var[] = $userID;
        }

        if (!empty($startDate)) {
            $where[] = 'createdate >= ?';
            $var[] = $startDate . ' 00:00:00';
        }
        if (!empty($endDate)) {
            $where[] = 'createdate <= ?';
            $var[] = $endDate . ' 23:59:59';
        }
        if (isset($resumeID) && !empty($resumeID)) {
            $where[] = 'resumeid = ?';
            $var[] = $resumeID;
        }

        if (!empty($where)) {
            $sql .= ' WHERE ' . implode(' AND ', $where);
        }


        $sql .= ' ORDER BY id DESC';

        $resumeList = Db::select($sql, $var, $rowFrom, $rowTo);

        if (empty($resumeList)) {
            return array();
        }

        $listToGetInfo = array();
        foreach ($resumeList as $key => $value) {
            $listToGetInfo[] = $value['resumeid'];
        }

        $managerModel = new Manager();
        $ResumeInfo = $managerModel->getManagerListByResumeId($listToGetInfo);
        foreach ($ResumeInfo as $key => $value) {
            foreach ($resumeList as $key2 => $value2) {
                if ($value['resumeid'] == $value2['resumeid']) {
                    $ResumeInfo[$key]['downloadtime'] = $value2['createdate'];
                }
            }
        }
        return $ResumeInfo;
    }

    public function getMyDownloadResumeCount($userID, $resumeID, $startDate, $endDate) {
        $sql = 'SELECT count(1) as count FROM ResumeDownload';
        $where = '';
        $var = [];

        //查询用户ID
        $where = $var = [];
        $where[] = ' oprid = ? ';
        $var[] = $userID;

        if (isset($resumeID) && is_numeric($resumeID)) {
            $tmpResume = array();
            $tmpResume[0] = array('count' => 1);
            return $tmpResume;
        } else {
            if (!empty($startDate)) {
                $where[] = 'createdate >= ?';
                $var[] = $startDate . ' 00:00:00';
            }
            if (!empty($endDate)) {
                $where[] = 'createdate <= ?';
                $var[] = $endDate . ' 23:59:59';
            }
        }

        if (!empty($where)) {
            $sql .= ' WHERE ' . implode(' AND ', $where);
        }

        return Db::select($sql, $var);
    }
}